<template>
	<view class="box">
		<view class="addAudio" contenteditable="true">
			<textarea class="input" maxlength="-1" placeholder="粘贴复制短视频链接" v-model="value"></textarea>
			<button class="clearButton" @click="clearText">清 空</button>
		</view>
		<view class="video">
			<view class="video_tle">短视频去水印</view>
			<view class="video_f">1、在短视频平台点击分享按钮，选择复制链接。</view>
			<view class="video_f">2、将视频链接粘贴到输入框中，点击去水印即可提取出无水印视频。</view>
			<view class="video_f">3、支持抖音、快手等热门短视频平台的视频去水印</view>
			<view class="video_f">4、保存到本地失败的情况下,请复制链接前往浏览器下载</view>
		</view>
		<button class="button" @click="video">去除视频水印</button>
		<!-- 非会员看广告提示 -->
		<u-modal :show="vipShow" :title="'温馨提示'" :content="'您还不是会员,此功能需开通会员后才可使用。'" cancelText="取消" confirmText="去开通"
			showCancelButton="true" @cancel="cancel" @confirm="govip" :closeOnClickOverlay="true"
			@close="vipShow=false"></u-modal>
		<u-popup :show="show" mode="center" @close="show = false" width="700rpx" height="1000rpx">
			<view class="content">
				<view class="content-a">
					<image src="../../static/icon/043.png" mode="widthFix" style="width: 100%;height: 100%;">
					</image>
				</view>
				<view class="content-b">
					解析成功
				</view>
				<view class="content-c">
					复制链接去浏览器下载,或直接保存到相册
				</view>
				<view class="content-c" style="font-size: 20rpx;color: red;">
					温馨提示:视频较大的情况请复制链接前往浏览器进行下载
				</view>
				<button class="buttons" @click="copy">复制链接</button>
				<button class="buttona" @click="download">保存到相册</button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		apiMixUrlApi,
		sslwxbdApi
	} from '@/api/assemb.js';
	import store from "../../store/idnex.js";
	// 在页面中定义激励视频广告
	let videoAd = null;
	var app = getApp();
	export default {
		data() {
			return {
				show: false,
				value: '',
				title: '修改名称',
				textValue: '短视频去水印',
				compound: {},
				isVip: false, //是否会员
				vipShow: false, // 激励广告是展示
				isAd: false, // 激励广告 观看是否成功
				ks: false,
				houzhui: '',
				copyurl: '',
				httpsUrl: ''
			};
		},
		onLoad() {
			this.ad();
		},
		onShow() {
			this.isVip = uni.getStorageSync('vipType').vipStatus == 1 ? true : false;
			// this.isVip = false;
		},
		methods: {
			close() {
				this.$refs.popup.close()
			},
			/**
			 * 广告初始化
			 */
			ad() {
				let _this = this;
				// 在页面onLoad回调事件中创建激励视频广告实例
				if (wx.createRewardedVideoAd) {
					videoAd = wx.createRewardedVideoAd({
						adUnitId: store.state.adUnitId
					});
					videoAd.onLoad(() => {});
					videoAd.onError(err => {});
					videoAd.onClose(res => {
						// 用户点击了【关闭广告】按钮
						if (res && res.isEnded) {
							_this.isAd = true;
							_this.video();
							// 正常播放结束，可以下发游戏奖励
						} else {
							// 播放中途退出，不下发游戏奖励
						}
					});
				}
			},
			/**
			 * 跳转充值页面
			 */
			govip() {
				this.vipShow = false;
				if (uni.getStorageSync('OS') !== 'ios') {
					uni.navigateTo({
						url: '/pages/paymentCenter/paymentCenter'
					});
				} else {
					if (getApp().globalData.iosPay == 0) {
						uni.showModal({
							title: '提示',
							content: '由于相关规范，IOS功能暂不可用',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					} else if (getApp().globalData.iosPay == 1) {
						uni.navigateTo({
							url: '/pages/goods/goodr'
						});
					}
				}
			},
			/**
			 * 观看广告触发
			 */
			cancel() {
				this.vipShow = false;
				// 用户触发广告后，显示激励视频广告
			},
			compounds() {
				var app = getApp();
				var that = this;
				let param = {
					url: that.value
				};
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				apiMixUrlApi(param, 1078, 'VideoParse').then(result => {
					console.log(result, 'ooooooooooooo11111')
					if (result.code == 200) {
						that.show = true
						that.copyurl = result.data.url
					} else {
						uni.showToast({
							title: result.descinfo,
							icon: 'none',
							duration: 1500
						});
					}
				});
			},
			download() {
				let that = this
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				if (that.copyurl.includes('https')) {
					that.httpsUrl = that.copyurl
				} else {
					that.httpsUrl = 'https:' + that.copyurl.slice(5)
					console.log('链接不包含 https');
				}
				let regex = /^(https?:\/\/[^\/]+)/;
				let requestAddress = that.httpsUrl.match(regex)[0];
				let pagesurl = [
					'https://txmov2.a.kwimgs.com',
					'https://upos-sz-mirrorcos.bilivideo.com',
					'https://v9-default.ixigua.com',
					'https://v1-default.ixigua.com',
					'https://v2-default.ixigua.com',
					'https://v3-default.ixigua.com',
					'https://v4-default.ixigua.com',
					'https://v5-default.ixigua.com',
					'https://v6-default.ixigua.com',
					'https://v7-default.ixigua.com',
					'https://v8-default.ixigua.com',
				]
				if (pagesurl.includes(requestAddress)) {
					console.log(requestAddress + ' 存在于 pagesurl 数组中');
					// that.share1(that.httpsUrl)
					that.share1('https://iflytts.oss-cn-qingdao.aliyuncs.com/MP4/2024/04/1914:51:09/171350946929098.mp4')
				} else {
					console.log(requestAddress + ' 不存在于 pagesurl 数组中');
					let param = {
						reqUrl: that.httpsUrl
					};
					sslwxbdApi(param, '1016', 'downloadVideo').then(res => {
						console.log(res, '文案提取')
						if (res.videoUrl != "") {
							let compound = {
								audioUrl: res.videoUrl
							};
							that.compound = compound;
							console.log(res.videoUrl, '这是抖音')
							that.share(res.videoUrl)
						}
					});

				}

			},
			//复制链接
			copy() {
				uni.setClipboardData({
					data: this.copyurl,
					showToast: true
				});
			},
			clearText() {
				let _this = this;
				console.log(_this.value);
				if (!_this.value) {
					uni.showToast({
						title: '输入框为空',
						icon: 'none',
						duration: 1500
					});
				} else {
					uni.showModal({
						title: '是否清空内容',
						success(res) {
							if (res.confirm) {
								console.log('用户点击确定');
								_this.value = '';
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			video() {
				if (this.value != "") {
					if (this.isAd || this.isVip) {
						this.isAd = false;
						this.compounds();
					} else {
						this.vipShow = true;
					}
				} else {
					uni.showToast({
						title: '内容为空，无法解析',
						icon: 'none',
						duration: 1500
					});
				}

			},
			share(url) {
				let _this = this;
				if (_this.ks) {
					const houzhu = url.match(/\.[^.]+?$/)[0]
					_this.houzhui = houzhu.slice(0, 4)
					console.log(_this.houzhui, '这是快手')
				} else {
					_this.houzhui = url.match(/\.[^.]+?$/)[0]
					console.log(_this.houzhui, '不是快手')
				}
				console.log(_this.houzhui)
				const name = '视频去水印'
				uni.showLoading({
					title: '下载中...',
					mask: true
				})
				wx.downloadFile({
					url: url, // 下载url
					filePath: wx.env.USER_DATA_PATH +
						"/weiYC" +
						name +
						_this.houzhui,
					success(reslut) {
						_this.ks = false
						console.log(reslut)
						// 下载完成后转发
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: '下载成功,点击保存',
							showCancel: false,
							confirmText: '保存',
							success: function(res) {
								console.log(res, 'res');
								if (res.confirm) {
									wx.saveVideoToPhotosAlbum({

										filePath: reslut.filePath,
										success: function(res) {
											uni.showToast({
												title: '保存成功',
												duration: 1500,
											})
											setTimeout(() => {
												_this.show = false
											}, 2000)

										},
										fail(err) {

											uni.showToast({
												title: '保存失败,请复制链接下载',
												icon: 'none',
												duration: 2000,
											})
											console.log(err, '00000')
											// setTimeout(() => {
											// 	_this.show = false
											// }, 2000)
										},
									})
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});

					},
					fail(err) {
						console.log(err)
						uni.showModal({
							title: '提示',
							content: '视频过大，请复制链接下载',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
						uni.hideLoading()
					},
					complete() {}
				});
			},
			//西瓜视频
			share1(url) {
				let _this = this;
				console.log(_this.houzhui)
				const name = '视频去水印'
				uni.showLoading({
					title: '下载中...',
					mask: true
				})
				wx.downloadFile({
					url: url, // 下载url
					success(reslut) {
						_this.ks = false
						console.log(reslut)
						// 下载完成后转发
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: '下载成功,点击保存',
							showCancel: false,
							confirmText: '保存',
							success: function(res) {
								console.log(res, 'res');
								if (res.confirm) {
									wx.saveVideoToPhotosAlbum({

										filePath: reslut.tempFilePath,
										success: function(res) {
											uni.showToast({
												title: '保存成功',
												duration: 1500,
											})
											setTimeout(() => {
												_this.show = false
											}, 2000)

										},
										fail(err) {
											uni.showToast({
												title: '保存失败,请复制链接下载',
												icon: 'none',
												duration: 2000,
											})
											console.log(err, '00000')
										},
									})
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});

					},
					fail(err) {
						console.log(err)
						uni.showModal({
							title: '提示',
							content: '视频过大，请复制链接下载',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
						uni.hideLoading()
					},
					complete() {}
				});
			},
		}
	};
</script>

<style lang="less" scoped>
	.box {
		padding-top: 30rpx;

		.content {
			width: 700rpx;
			height: 1000rpx;

			.content-a {
				width: 110px;
				height: 110px;
				margin-left: 248rpx;
				margin-top: 50rpx;
			}

			.content-b {
				font-weight: 900;
				margin-left: 280rpx;
				margin-top: 30rpx;
			}

			.content-c {
				font-size: 26rpx;
				color: #999999;
				margin-left: 100rpx;
				margin-top: 30rpx;
			}

			.buttons {
				margin-top: 200rpx;
				width: 544rpx;
				line-height: 96rpx;
				background: #5490ff;
				border-radius: 24px;
				color: #ffffff;
			}

			.buttona {
				margin-top: 50rpx;
				width: 544rpx;
				line-height: 96rpx;
				background: #5490ff;
				border-radius: 24px;
				color: #ffffff;
			}
		}

		.addAudio {
			width: 678rpx;
			height: 340rpx;
			margin: 0 auto;
			border: 1px dashed #5490FF;
			position: relative;
			overflow: hidden;
			border-radius: 18rpx;

			.input {
				width: 620rpx;
				margin: 0 auto;
				color: #999999;
				overflow: visible;
				word-break: break-all;
				padding: 20rpx 35rpx;
				vertical-align: top;
				outline: none;
			}

			.clearButton {
				width: 120rpx;
				line-height: 55rpx;
				font-size: 28rpx;
				color: #ffffff;
				background: #999992;
				text-align: center;
				position: absolute;
				bottom: 20rpx;
				right: 20rpx;
				z-index: 33;
			}
		}

		.video {
			display: flex;
			flex-direction: column;
			width: 640rpx;
			margin: 0 auto;
			padding-top: 66rpx;

			.video_tle {
				font-size: 30rpx;
				padding-bottom: 28rpx;
			}

			.video_f {
				font-size: 26rpx;
				color: #999999;
				line-height: 46rpx;
			}
		}

		.button {
			width: 544rpx;
			line-height: 96rpx;
			background: #5490FF;
			border-radius: 24px;
			color: #ffffff;
			position: absolute;
			bottom: 80rpx;
			left: 104rpx;
		}
	}
</style>
<style>
	page {
		background-color: #f1f1f1;
	}
</style>